package UserTable;

import user.User;
import utils.JdbcUtils;

import javax.swing.*;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class QueryUser {
    public static void init(){
        JFrame frame=  new JFrame("查询");
        String info = JOptionPane.showInputDialog(frame,"你要查询的用户名为:","查询",JOptionPane.WARNING_MESSAGE);
        List<Map<String, Object>> re= JdbcUtils.JdbcQuery(User.sql_query,info);
        if(!re.isEmpty()){
            JOptionPane.showMessageDialog(frame,"查询成功");
            int id = 0;
            String name = null;
            int power=0;
            LocalDateTime timebe=null;
            LocalDateTime timeup=null;
            for(Map<String,Object> map : re){
                for(String key : map.keySet()) {
                    if (key.equals("user_id")) {
                        id = (int) map.get(key);
                    }
                    if (key.equals("user_name")) {
                        name = (String) map.get(key);
                    }
                    if (key.equals("user_create_time")) {
                        Object createTimeObj = map.get(key);
                        if (createTimeObj instanceof Timestamp) {
                            Timestamp createTime = (Timestamp) createTimeObj;
                            timebe = createTime.toLocalDateTime();
                        }
                    }
                    if (key.equals("user_lastup_time")) {
                        Object lastUpTimeObj = map.get(key);
                        if (lastUpTimeObj instanceof Timestamp) {
                            Timestamp lastUpTime = (Timestamp) lastUpTimeObj;
                            timeup = lastUpTime.toLocalDateTime();
                        }
                    }
                    if (key.equals("user_power")) {
                        power = (int) map.get(key);
                    }
                }
            }
            JOptionPane.showMessageDialog(frame,"用户名id为:"+id+"\n用户名为:"+name+"\n用户创建时间:"+timebe+"\n用户最后登录时间"+timeup+"\n用户权限"+power);
        }
    }
}
